package com.example.warehouse.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.warehouse.bus.dao.persist.mapper.BusRepertoryMapper;
import com.example.warehouse.bus.pojo.entity.BusRepertory;
import com.example.warehouse.bus.pojo.param.BusRepertoryAddGoodsNum;
import com.example.warehouse.bus.pojo.param.BusRepertoryReduceGoodsNumParam;
import com.example.warehouse.bus.pojo.vo.BusRepertoryInfoVo;
import com.example.warehouse.bus.service.IBusRepertoryService;
import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.util.PageInfoToPageDataConverter;
import com.example.warehouse.common.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class IBusRepertoryServiceImpl implements IBusRepertoryService {
    @Autowired
    private BusRepertoryMapper brm;

    /**
     * 处理的业务逻辑
     * @param busRepertoryAddGoodsNum
     * 1.首先需要判断商品名是否在数据库中存在，若存在只需要进行一个增加商品数量的操作同时进行修改时间的更新
     * 若不存在，则需要将传进来的所有数据进行一个插入操作,创建时间和修改时间时间全为当前时间
     *
     * * */
    @Override
    public void addGoodsNum(BusRepertoryAddGoodsNum busRepertoryAddGoodsNum) {
        String goodsName = brm.findBuGoodsIdToGoodsName(busRepertoryAddGoodsNum.getGoodId());
        QueryWrapper<BusRepertory> qw=new QueryWrapper<>();
        qw.eq("goods_name",goodsName);
        Integer findGoodsNameCount = brm.selectCount(qw);
        if(findGoodsNameCount>0)//这里表示的数据库存在该商品名，下面做一个增加商品数量和修改时间的更新
        {
            //增加商品数量
            brm.AddGoodsNum(busRepertoryAddGoodsNum.getGoodsNum(),goodsName);
           //这里先将该商品数据查询出来，在进行更新时间
            BusRepertory goodData = brm.findBuGoodsNameData(goodsName);
            goodData.setGmtModified(LocalDateTime.now());
            brm.updateById(goodData);
        }else{
            BusRepertory busRepertory=new BusRepertory();
            BeanUtils.copyProperties(busRepertoryAddGoodsNum,busRepertory);
            busRepertory.setGoodsName(goodsName);
            busRepertory.setGmtCreate(LocalDateTime.now());
            busRepertory.setGmtModified(LocalDateTime.now());
            //第一次添加，做一个全部插入的操作
            brm.insert(busRepertory);
        }

    }

    @Override
    public void reduceGoodsNum(BusRepertoryReduceGoodsNumParam busRepertoryReduceGoodsNumParam) {
        //先判断库存数量是否够订单的需求量
        BusRepertory goodsData = brm.findBuGoodsNameData(busRepertoryReduceGoodsNumParam.getGoodsName());
        if(goodsData.getGoodsNum()<busRepertoryReduceGoodsNumParam.getGoodsNum())
        {
            String message="此次出货失败,库存数不足!,该增加库存了";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_BAD_REQUEST,message);
        }
        //下面直接执行减少库存的操作，因为商品肯定在数据库中是存在的
        brm.reduceGoodsNum(busRepertoryReduceGoodsNumParam.getGoodsNum(),busRepertoryReduceGoodsNumParam.getGoodsName());
        //判断报警预值是否小于库存数量
        if(goodsData.getDangerNum()>=goodsData.getGoodsNum())
        {
            String message="库存快不足了，需要进货啦!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
        //更新修改时间
        BusRepertory updateGoodsData = brm.findBuGoodsNameData(busRepertoryReduceGoodsNumParam.getGoodsName());
        updateGoodsData.setGmtModified(LocalDateTime.now());
        brm.updateById(updateGoodsData);
    }

    @Override
    public void deleteByIdGoods(Long id) {
        BusRepertory goodsData = brm.selectById(id);
        if(goodsData==null)
        {
            String message="删除失败,该条数据已经不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        int deleteGoodsCount = brm.deleteById(id);
        if(deleteGoodsCount==0)
        {
            String message="删除失败,服务器忙，请稍后尝试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UNKNOWN,message);
        }
    }

    @Override
    public PageData<BusRepertoryInfoVo> pageHelperByPageNum(Integer pageNum) {
        Integer pageSize=10;
        return pageHelperByPageNumAndPageSize(pageNum,pageSize);
    }

    @Override
    public PageData<BusRepertoryInfoVo> pageHelperByPageNumAndPageSize(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<BusRepertoryInfoVo> list = brm.list();
        PageInfo<BusRepertoryInfoVo> pageInfo=new PageInfo<>(list);
       return PageInfoToPageDataConverter.convert(pageInfo);
    }

    /**
     * 模糊查询后的分页查询
     * @param pageNum
     * @param keyword
     * @return
     */
    @Override
    public PageData<BusRepertoryInfoVo> pageHelperByPageNumAndGoodsNameFuzzyQuery(Integer pageNum, String keyword) {
       Integer pageSize=10;
       PageHelper.startPage(pageNum,pageSize);
        List<BusRepertoryInfoVo> list = brm.fuzzyQueryList(keyword);
        PageInfo<BusRepertoryInfoVo> pageInfo=new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
}
